/*
 * OPIAM Suite
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package opiam.admin.faare.unittests.service.javabeans;

import opiam.admin.faare.PropertiesManager;
import opiam.admin.faare.service.UserContext;
import opiam.admin.faare.service.javabeans.Criteria;
import opiam.admin.faare.service.services.StandardService;
import opiam.admin.faare.unittests.TestResult;
import opiam.admin.faare.unittests.TestUtils;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

import org.apache.log4j.Logger;


/**
 */
public class CriteriaTest extends TestCase
{
    /** Instance of logger. */
    private static Logger _logger = Logger.getLogger(CriteriaTest.class);

    /** Instance of the trace class. */
    private static TestUtils _test = new TestUtils(_logger);

    /** Test user DN. */
    private static final String DN_USER = "uid=jreuter, ou=People, dc=mycompany,dc=com";

    /** Test user login. */
    private static final String LOGIN_USER = "jreuter"; //uid

    /** Test user password. */
    private static final String PASSWORD_USER = "destroy";

    /** User context. */
    private static UserContext _USERCONTEXT = new UserContext();

    /**
     * Creates a new CriteriaTest object.
     *
     * @param param  Parameter.
     */
    public CriteriaTest(String param)
    {
        super(param);

        try
        {
            PropertiesManager.getInstance();

            if (!_USERCONTEXT.isLoggedIn())
            {
                StandardService.logon(LOGIN_USER, PASSWORD_USER, _USERCONTEXT);
            }
        }
        catch (Exception e)
        {
            StringBuffer msg = new StringBuffer();
            msg.append("CriteriaTest constructor error");
            msg.append(TestUtils.NEXT);
            msg.append(e.toString());
            _test.displayResultError(msg.toString());
        }
    }

    /**
     * Method of the tests suite.
     *
     * @return Tests result.
     */
    public static Test suite()
    {
        TestSuite suite = new TestSuite();

        suite.addTest(new CriteriaTest("testBegin"));

        // TESTS SET
        suite.addTest(new CriteriaTest("testBeginsWith"));
        suite.addTest(new CriteriaTest("testEndsWith"));
        suite.addTest(new CriteriaTest("testContains"));
        suite.addTest(new CriteriaTest("testEqualsTo"));
        suite.addTest(new CriteriaTest("testNotContains"));
        suite.addTest(new CriteriaTest("testNotEqualsTo"));
        suite.addTest(new CriteriaTest("testApproxEquals"));
        suite.addTest(new CriteriaTest("testGreaterThan"));
        suite.addTest(new CriteriaTest("testLessThan"));
        suite.addTest(new CriteriaTest("testExists"));
        suite.addTest(new CriteriaTest("testNotExists"));
        suite.addTest(new CriteriaTest("testLessOrEqualThan"));
        suite.addTest(new CriteriaTest("testGreaterOrEqualThan"));
        suite.addTest(new CriteriaTest("testAndMultipleCriteria"));
        suite.addTest(new CriteriaTest("testOrMultipleCriteria"));
        suite.addTest(new CriteriaTest("testMixedOrAndCriteria"));

        suite.addTest(new CriteriaTest("testEnd"));

        return (suite);
    }

    /**
     * Main method of the test class.
     *
     * @param args  Sets of the parameters.
     */
    public static void main(String[] args)
    {
        junit.textui.TestRunner.run(suite());
    }

    /**
     * Method to indicate the beginning of the class test.
     *
     */
    public static void testBegin()
    {
        //  To initialize the tests statement
        TestResult.getInstance().setInitiator("CriteriaTest");

        _test.displayHeaderClass("CriteriaTest");
        assertTrue(true);
    }

    /**
     * Method to indicate the end of the class test.
     *
     */
    public static void testEnd()
    {
        _test.displayFooterClass(null);
        assertTrue(true);

        // To display the tests statement
        _test.displayTestStatement("CriteriaTest");
    }

    /**
     * Tests BeginsWith.
     */
    public void testBeginsWith()
    {
        _test.displayHeaderMethod("testBeginsWith");

        try
        {
            String expectedFilter = "(sn=Farmer*)";

            Criteria c1 = Criteria.getAndInstance();
            c1.addBeginsWith("nom", "Farmer");

            String filter = c1.getLdapFilter(_USERCONTEXT.findJBRessourceByName(
                        "person"), _USERCONTEXT);
            _test.displayMsg("Expected filter : " + expectedFilter);

            if (filter.equals(expectedFilter))
            {
                _test.displayResultSuccess("Obtained filter : " + filter);
                assertTrue(true);
            }
            else
            {
                _test.displayResultFailure("Obtained filter : " + filter);
                assertTrue(false);
            }
        }
        catch (Exception ex)
        {
            StringBuffer msg = new StringBuffer();
            msg.append("Exception : ");
            msg.append(TestUtils.NEXT);
            msg.append(ex.toString());
            _test.displayResultError(msg.toString());
        }

        _test.displayFooterMethod("testBeginsWith");
    }

    /**
     * Tests EndsWith.
     */
    public void testEndsWith()
    {
        _test.displayHeaderMethod("testEndsWith");

        try
        {
            String expectedFilter = "(sn=*Farmer)";

            Criteria c1 = Criteria.getAndInstance();
            c1.addEndsWith("nom", "Farmer");

            String filter = c1.getLdapFilter(_USERCONTEXT.findJBRessourceByName(
                        "person"), _USERCONTEXT);
            _test.displayMsg("Expected filter : " + expectedFilter);

            if (filter.equals(expectedFilter))
            {
                _test.displayResultSuccess("Obtained filter : " + filter);
                assertTrue(true);
            }
            else
            {
                _test.displayResultFailure("Obtained filter : " + filter);
                assertTrue(false);
            }
        }
        catch (Exception ex)
        {
            StringBuffer msg = new StringBuffer();
            msg.append("Exception : ");
            msg.append(TestUtils.NEXT);
            msg.append(ex.toString());
            _test.displayResultError(msg.toString());
        }

        _test.displayFooterMethod("testEndsWith");
    }

    /**
     * Tests contains.
     */
    public void testContains()
    {
        _test.displayHeaderMethod("testContains");

        try
        {
            String expectedFilter = "(sn=*Farmer*)";

            Criteria c1 = Criteria.getAndInstance();
            c1.addContains("nom", "Farmer");

            String filter = c1.getLdapFilter(_USERCONTEXT.findJBRessourceByName(
                        "person"), _USERCONTEXT);
            _test.displayMsg("Expected filter : " + expectedFilter);

            if (filter.equals(expectedFilter))
            {
                _test.displayResultSuccess("Obtained filter : " + filter);
                assertTrue(true);
            }
            else
            {
                _test.displayResultFailure("Obtained filter : " + filter);
                assertTrue(false);
            }
        }
        catch (Exception ex)
        {
            StringBuffer msg = new StringBuffer();
            msg.append("Exception : ");
            msg.append(TestUtils.NEXT);
            msg.append(ex.toString());
            _test.displayResultError(msg.toString());
        }

        _test.displayFooterMethod("testContains");
    }

    /**
     * Tests equalsTo.
     */
    public void testEqualsTo()
    {
        _test.displayHeaderMethod("testEqualsTo");

        try
        {
            String expectedFilter = "(sn=Farmer)";

            Criteria c1 = Criteria.getAndInstance();
            c1.addEqualsTo("nom", "Farmer");

            String filter = c1.getLdapFilter(_USERCONTEXT.findJBRessourceByName(
                        "person"), _USERCONTEXT);
            _test.displayMsg("Expected filter : " + expectedFilter);

            if (filter.equals(expectedFilter))
            {
                _test.displayResultSuccess("Obtained filter : " + filter);
                assertTrue(true);
            }
            else
            {
                _test.displayResultFailure("Obtained filter : " + filter);
                assertTrue(false);
            }
        }
        catch (Exception ex)
        {
            StringBuffer msg = new StringBuffer();
            msg.append("Exception : ");
            msg.append(TestUtils.NEXT);
            msg.append(ex.toString());
            _test.displayResultError(msg.toString());
        }

        _test.displayFooterMethod("testEqualsTo");
    }

    /**
     * Test NotContains.
     */
    public void testNotContains()
    {
        _test.displayHeaderMethod("testNotContains");

        try
        {
            String expectedFilter = "(!(sn=*farmer*))";
            Criteria c1 = Criteria.getAndInstance();
            c1.addNotContains("nom", "farmer");

            String filter = c1.getLdapFilter(_USERCONTEXT.findJBRessourceByName(
                        "person"), _USERCONTEXT);
            _test.displayMsg("Expected filter : " + expectedFilter);

            if (filter.equals(expectedFilter))
            {
                _test.displayResultSuccess("Obtained filter : " + filter);
                assertTrue(true);
            }
            else
            {
                _test.displayResultFailure("Obtained filter : " + filter);
                assertTrue(false);
            }
        }
        catch (Exception ex)
        {
            StringBuffer msg = new StringBuffer();
            msg.append("Exception : ");
            msg.append(TestUtils.NEXT);
            msg.append(ex.toString());
            _test.displayResultError(msg.toString());
        }

        _test.displayFooterMethod("testNotContains");
    }

    /**
     * Tests notEquals.
     */
    public void testNotEqualsTo()
    {
        _test.displayHeaderMethod("testNotEqualsTo");

        try
        {
            String expectedFilter = "(!(sn=farmer))";
            Criteria c1 = Criteria.getAndInstance();
            c1.addNotEqualsTo("nom", "farmer");

            String filter = c1.getLdapFilter(_USERCONTEXT.findJBRessourceByName(
                        "person"), _USERCONTEXT);
            _test.displayMsg("Expected filter : " + expectedFilter);

            if (filter.equals(expectedFilter))
            {
                _test.displayResultSuccess("Obtained filter : " + filter);
                assertTrue(true);
            }
            else
            {
                _test.displayResultFailure("Obtained filter : " + filter);
                assertTrue(false);
            }
        }
        catch (Exception ex)
        {
            StringBuffer msg = new StringBuffer();
            msg.append("Exception : ");
            msg.append(TestUtils.NEXT);
            msg.append(ex.toString());
            _test.displayResultError(msg.toString());
        }

        _test.displayFooterMethod("testNotEqualsTo");
    }

    /**
     * Tests approxEquals.
     */
    public void testApproxEquals()
    {
        _test.displayHeaderMethod("testApproxEquals");

        try
        {
            String expectedFilter = "(sn~=Farmer)";
            Criteria c1 = Criteria.getAndInstance();
            c1.addApproximatelyEqualsTo("nom", "Farmer");

            String filter = c1.getLdapFilter(_USERCONTEXT.findJBRessourceByName(
                        "person"), _USERCONTEXT);
            _test.displayMsg("Expected filter : " + expectedFilter);

            if (filter.equals(expectedFilter))
            {
                _test.displayResultSuccess("Obtained filter : " + filter);
                assertTrue(true);
            }
            else
            {
                _test.displayResultFailure("Obtained filter : " + filter);
                assertTrue(false);
            }
        }
        catch (Exception ex)
        {
            StringBuffer msg = new StringBuffer();
            msg.append("Exception : ");
            msg.append(TestUtils.NEXT);
            msg.append(ex.toString());
            _test.displayResultError(msg.toString());
        }

        _test.displayFooterMethod("testApproxEquals");
    }

    /**
     * Tests greaterThan.
     */
    public void testGreaterThan()
    {
        _test.displayHeaderMethod("testGreaterThan");

        try
        {
            String expectedFilter = "(!(sn<=farmer))";
            Criteria c1 = Criteria.getAndInstance();
            c1.addGreaterThan("nom", "farmer");

            String filter = c1.getLdapFilter(_USERCONTEXT.findJBRessourceByName(
                        "person"), _USERCONTEXT);
            _test.displayMsg("Expected filter : " + expectedFilter);

            if (filter.equals(expectedFilter))
            {
                _test.displayResultSuccess("Obtained filter : " + filter);
                assertTrue(true);
            }
            else
            {
                _test.displayResultFailure("Obtained filter : " + filter);
                assertTrue(false);
            }
        }
        catch (Exception ex)
        {
            StringBuffer msg = new StringBuffer();
            msg.append("Exception : ");
            msg.append(TestUtils.NEXT);
            msg.append(ex.toString());
            _test.displayResultError(msg.toString());
        }

        _test.displayFooterMethod("testGreaterThan");
    }

    /**
     * Tests lessThan.
     */
    public void testLessThan()
    {
        _test.displayHeaderMethod("testLessThan");

        try
        {
            String expectedFilter = "(!(sn>=farmer))";
            Criteria c1 = Criteria.getAndInstance();
            c1.addLessThan("nom", "farmer");

            String filter = c1.getLdapFilter(_USERCONTEXT.findJBRessourceByName(
                        "person"), _USERCONTEXT);
            _test.displayMsg("Expected filter : " + expectedFilter);

            if (filter.equals(expectedFilter))
            {
                _test.displayResultSuccess("Obtained filter : " + filter);
                assertTrue(true);
            }
            else
            {
                _test.displayResultFailure("Obtained filter : " + filter);
                assertTrue(false);
            }
        }
        catch (Exception ex)
        {
            StringBuffer msg = new StringBuffer();
            msg.append("Exception : ");
            msg.append(TestUtils.NEXT);
            msg.append(ex.toString());
            _test.displayResultError(msg.toString());
        }

        _test.displayFooterMethod("testLessThan");
    }

    /**
     * Tests exists.
     */
    public void testExists()
    {
        _test.displayHeaderMethod("testExists");

        try
        {
            String expectedFilter = "(sn=*)";
            Criteria c1 = Criteria.getAndInstance();
            c1.addExists("nom");

            String filter = c1.getLdapFilter(_USERCONTEXT.findJBRessourceByName(
                        "person"), _USERCONTEXT);
            _test.displayMsg("Expected filter : " + expectedFilter);

            if (filter.equals(expectedFilter))
            {
                _test.displayResultSuccess("Obtained filter : " + filter);
                assertTrue(true);
            }
            else
            {
                _test.displayResultFailure("Obtained filter : " + filter);
                assertTrue(false);
            }
        }
        catch (Exception ex)
        {
            StringBuffer msg = new StringBuffer();
            msg.append("Exception : ");
            msg.append(TestUtils.NEXT);
            msg.append(ex.toString());
            _test.displayResultError(msg.toString());
        }

        _test.displayFooterMethod("testExists");
    }

    /**
     * Tests notExists.
     */
    public void testNotExists()
    {
        _test.displayHeaderMethod("testNotExists");

        try
        {
            String expectedFilter = "(!(sn=*))";
            Criteria c1 = Criteria.getAndInstance();
            c1.addNotExists("nom");

            String filter = c1.getLdapFilter(_USERCONTEXT.findJBRessourceByName(
                        "person"), _USERCONTEXT);
            _test.displayMsg("Expected filter : " + expectedFilter);

            if (filter.equals(expectedFilter))
            {
                _test.displayResultSuccess("Obtained filter : " + filter);
                assertTrue(true);
            }
            else
            {
                _test.displayResultFailure("Obtained filter : " + filter);
                assertTrue(false);
            }
        }
        catch (Exception ex)
        {
            StringBuffer msg = new StringBuffer();
            msg.append("Exception : ");
            msg.append(TestUtils.NEXT);
            msg.append(ex.toString());
            _test.displayResultError(msg.toString());
        }

        _test.displayFooterMethod("testNotExists");
    }

    /**
     * Tests lessOrEqualThan.
     */
    public void testLessOrEqualThan()
    {
        _test.displayHeaderMethod("testLessOrEqualThan");

        try
        {
            String expectedFilter = "(sn<=farmer)";
            Criteria c1 = Criteria.getAndInstance();
            c1.addLessOrEqualThan("nom", "farmer");

            String filter = c1.getLdapFilter(_USERCONTEXT.findJBRessourceByName(
                        "person"), _USERCONTEXT);
            _test.displayMsg("Expected filter : " + expectedFilter);

            if (filter.equals(expectedFilter))
            {
                _test.displayResultSuccess("Obtained filter : " + filter);
                assertTrue(true);
            }
            else
            {
                _test.displayResultFailure("Obtained filter : " + filter);
                assertTrue(false);
            }
        }
        catch (Exception ex)
        {
            StringBuffer msg = new StringBuffer();
            msg.append("Exception : ");
            msg.append(TestUtils.NEXT);
            msg.append(ex.toString());
            _test.displayResultError(msg.toString());
        }

        _test.displayFooterMethod("testLessOrEqualThan");
    }

    /**
     * Tests greaterOrEqualThan.
     */
    public void testGreaterOrEqualThan()
    {
        _test.displayHeaderMethod("testGreaterOrEqualThan");

        try
        {
            String expectedFilter = "(sn>=farmer)";
            Criteria c1 = Criteria.getAndInstance();
            c1.addGreaterOrEqualThan("nom", "farmer");

            String filter = c1.getLdapFilter(_USERCONTEXT.findJBRessourceByName(
                        "person"), _USERCONTEXT);
            _test.displayMsg("Expected filter : " + expectedFilter);

            if (filter.equals(expectedFilter))
            {
                _test.displayResultSuccess("Obtained filter : " + filter);
                assertTrue(true);
            }
            else
            {
                _test.displayResultFailure("Obtained filter : " + filter);
                assertTrue(false);
            }
        }
        catch (Exception ex)
        {
            StringBuffer msg = new StringBuffer();
            msg.append("Exception : ");
            msg.append(TestUtils.NEXT);
            msg.append(ex.toString());
            _test.displayResultError(msg.toString());
        }

        _test.displayFooterMethod("testGreaterOrEqualThan");
    }

    /**
     * Tests AndMultipleCriteria.
     */
    public void testAndMultipleCriteria()
    {
        _test.displayHeaderMethod("testAndMultipleCriteria");

        try
        {
            String expectedFilter = "(&(sn=farmer)(givenname=*)(mail=*farmer*))";
            Criteria c1 = Criteria.getAndInstance();
            c1.addEqualsTo("nom", "farmer");
            c1.addExists("prenom");
            c1.addContains("mail", "farmer");

            String filter = c1.getLdapFilter(_USERCONTEXT.findJBRessourceByName(
                        "person"), _USERCONTEXT);
            _test.displayMsg("Expected filter : " + expectedFilter);

            if (filter.equals(expectedFilter))
            {
                _test.displayResultSuccess("Obtained filter : " + filter);
                assertTrue(true);
            }
            else
            {
                _test.displayResultFailure("Obtained filter : " + filter);
                assertTrue(false);
            }
        }
        catch (Exception ex)
        {
            StringBuffer msg = new StringBuffer();
            msg.append("Exception : ");
            msg.append(TestUtils.NEXT);
            msg.append(ex.toString());
            _test.displayResultError(msg.toString());
        }

        _test.displayFooterMethod("testAndMultipleCriteria");
    }

    /**
     * Tests OrMultipleCriteria.
     */
    public void testOrMultipleCriteria()
    {
        _test.displayHeaderMethod("testOrMultipleCriteria");

        try
        {
            String expectedFilter = "(|(sn=farmer)(givenname=*)(mail=farmer))";
            Criteria c1 = Criteria.getOrInstance();
            c1.addEqualsTo("nom", "farmer");
            c1.addExists("prenom");
            c1.addEqualsTo("mail", "farmer");

            String filter = c1.getLdapFilter(_USERCONTEXT.findJBRessourceByName(
                        "person"), _USERCONTEXT);
            _test.displayMsg("Expected filter : " + expectedFilter);

            if (filter.equals(expectedFilter))
            {
                _test.displayResultSuccess("Obtained filter : " + filter);
                assertTrue(true);
            }
            else
            {
                _test.displayResultFailure("Obtained filter : " + filter);
                assertTrue(false);
            }
        }
        catch (Exception ex)
        {
            StringBuffer msg = new StringBuffer();
            msg.append("Exception : ");
            msg.append(TestUtils.NEXT);
            msg.append(ex.toString());
            _test.displayResultError(msg.toString());
        }

        _test.displayFooterMethod("testOrMultipleCriteria");
    }

    /**
     * Tests MixedOrAndCriteria.
     */
    public void testMixedOrAndCriteria()
    {
        _test.displayHeaderMethod("testMixedOrAndCriteria");

        try
        {
            String expectedFilter = "(|(&(sn=farmer)(givenname=gern))(&(sn=ward)(givenname=*)))";
            Criteria c1 = Criteria.getAndInstance();
            c1.addEqualsTo("nom", "farmer");
            c1.addEqualsTo("prenom", "gern");

            Criteria c2 = Criteria.getAndInstance();
            c2.addEqualsTo("nom", "ward");
            c2.addExists("prenom");

            Criteria c3 = Criteria.getOrInstance();
            c3.addCriteria(c1);
            c3.addCriteria(c2);

            String filter = c3.getLdapFilter(_USERCONTEXT.findJBRessourceByName(
                        "person"), _USERCONTEXT);
            _test.displayMsg("Expected filter : " + expectedFilter);

            if (filter.equals(expectedFilter))
            {
                _test.displayResultSuccess("Obtained filter : " + filter);
                assertTrue(true);
            }
            else
            {
                _test.displayResultFailure("Obtained filter : " + filter);
                assertTrue(false);
            }
        }
        catch (Exception ex)
        {
            StringBuffer msg = new StringBuffer();
            msg.append("Exception : ");
            msg.append(TestUtils.NEXT);
            msg.append(ex.toString());
            _test.displayResultError(msg.toString());
        }

        _test.displayFooterMethod("testMixedOrAndCriteria");
    }
}
